<?php

/**
 * @author honestphan
 * @copyright 2012
 */
 
 //G?i t?p tin giao di?n trong thu m?c view/theme/template tuong ?ng
include('model/m_quangcao.php');
include('controller/c_thongke.php');
$page = $_GET['act'];
 switch($page){
    case 'list':
    
     if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
     
      if(!$_SESSION['type']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
     
    //Get language
    require('language/quangcao/list.php');
    $heading_title = heading_title;
    $title = heading_title;
    $button_add = button_add;
    $button_edit = button_edit;
    $button_delete = button_delete;
   
    $link_add ='index.php?module=quangcao&act=add';

     if($_POST['selected']==""){
        $err['exist'] = chua_chon;
        $error_name = $err['exist'];
        }
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    
    // kiem tra bien pages
    if($_REQUEST['page']==""){ $page = 1;}
    else{$page = $_REQUEST['page'];}
    
    //get input for class page
      $limit = 10;
      $start = ($page - 1) * $limit; 
    
    
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid){
      foreach($_POST['selected'] as $id){
        delQuangCao($id);
        $ref="http://".eregi_replace("/$","",(getenv(HTTP_HOST)?getenv(HTTP_HOST):$_SERVER['HTTP_HOST']).(getenv(REQUEST_URI)?getenv(REQUEST_URI):$_SERVER['REQUEST_URI']));
        //writeLog("Xóa quảng cáo - mã".$id,$ref);
      }  
      $_SESSION['success'] = del_success;
    //chuyen huong ve list
    header('LOCATION: '.$link_list);
     }

    $list_quangcao = getListQuangCao($start,$limit);
     $total = total();
       $paging = new Pagination();
       $paging->total = $total;
       $paging->limit = $limit;
       $paging->url = "index.php?module=quangcao&act=list&page={page}";
       $paging->page = $page;
       $pagination = $paging->render();


    require('view/template/quangcao/list.tpl');  
    break;
    
    
    
    //case them can bo
    case 'add':
    
     if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
     }
     
    if($_SESSION['type']=='cb'){
         header('LOCATION: index.php?module=home');  
         exit();
    }
     require ('language/quangcao/add.php');
     $heading_title = heading_title;
     $title = heading_title;
     
     $active = active;
     $deactive = deactive;
     $trang_thai = trang_thai;
     $list_loaiquangcao = getListLoaiQuangCao(0,100);
    
     $link_list = 'index.php?module=quangcao&act=list';
     $_SESSION['ten'] = $_POST['ten'];
      
   
       if($_POST['ten']==""){
        $err['exist'] = nhap_ten;
        $error_dn = $err['exist'];
        
       }
   
    if(!$err){
        $valid = true;    
    }
    else{
        $valid = false;
    }
    
    //kiem tra du lieu cua from
    if(($_SERVER['REQUEST_METHOD']=='POST')&&$valid)
    {
      $_POST['edit_date'] = time();
    insertQuangCao($_POST);
    $_SESSION['success'] = edit_success;
    //ghi nhat ki
     $ref="http://".eregi_replace("/$","",(getenv(HTTP_HOST)?getenv(HTTP_HOST):$_SERVER['HTTP_HOST']).(getenv(REQUEST_URI)?getenv(REQUEST_URI):$_SERVER['REQUEST_URI']));
    // writeLog("Thêm quảng cáo ".$_POST['ten']."-".$_POST['ten'],$ref);
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
    
 
    require('view/template/quangcao/edit.tpl');
    break;
    
    
    //case them can bo
    case 'edit':
   
    if(!$_SESSION['loged']){
         header('LOCATION: index.php?module=canbo&act=login');  
         exit();
    }
    
    if($_SESSION['type']=='cb'){
         header('LOCATION: index.php?module=home');  
         exit();
    }
   
    
     require ('language/quangcao/edit.php');
     $title = heading_title;
     $heading_title = heading_title;
     
     $trang_thai = trang_thai;
     
     $active = active;
     $deactive = deactive;
       $list_loaiquangcao = getListLoaiQuangCao(0,100);
    $link_list = 'index.php?module=quangcao&act=list';
    $id = $_REQUEST['id'];
    $quangcao = getQuangCao($id);
    
      //kiem tra su ton tai
    
    if($_SERVER['REQUEST_METHOD']=='POST')
    {
     $_POST['edit_date'] = time();
     editQuangCao($id, $_POST);
    $_SESSION['success'] = edit_success;
    //ghi log
    $ref="http://".eregi_replace("/$","",(getenv(HTTP_HOST)?getenv(HTTP_HOST):$_SERVER['HTTP_HOST']).(getenv(REQUEST_URI)?getenv(REQUEST_URI):$_SERVER['REQUEST_URI']));
    //writeLog("Sửa thông tin quảng cáo".$id."-".$canbo['ten_can_bo'],$ref);
    //chuyen huong ve list
    header('LOCATION: '.$link_list);    
    }
    require('view/template/quangcao/edit.tpl');
    break;
    
   //update my prolife
    
    
  
   }
 
?>